Passed
Push — main ( 31d92c...b21012 )
by Stefan
17:18
created

FormGenerator.js ➔ rangeChanged   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
1
/** global: g_oConfigFromPHP */
2
/** global: FormDataValidator */
3
/** global: jscolor */
4
/** global: CKEDITOR */
5
/** global: FormCKEditor */
6
/** global: RichFmConnector */
7
/** global: FormPicker */
8
9
var g_oCKEdit = null;
10
var g_FDP = null;
11
12
/**
13
 * post load some additional JS scripts after DOM is loaded...
14
 */
15
document.addEventListener('DOMContentLoaded', loadScriptfiles);
16
17
function loadScriptfiles()
18
{
19
    loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormDataValidator.js');
20
    if (g_oConfigFromPHP.Color !== undefined) {
21
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'jscolor.js');
22
    }
23
    
24
    if (g_oConfigFromPHP.DTSel !== undefined) {
25
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'dtsel.js');
26
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormPicker.js');
27
    }
28
29
    if (g_oConfigFromPHP.RichFilemanager !== undefined) {
30
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'RichFmConnector.js');
31
    }
32
    
33
    if (g_oConfigFromPHP.CKEditor !== undefined) {
34
        loadScript(g_oConfigFromPHP.CKEditor.Path + 'ckeditor.js');
35
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormCKEdit.js');
36
    }
37
}
38
39
/**
40
 * Initialization.
41
 * For initialization we lisen to the window onload-event. At this point the 
42
 * additionally loaded script files from the DOMContentLoaded-event should also be 
43
 * available. 
44
 * Dependent on the config from PHP we have to initialize some modules:
45
 * - FormDataValidation needs no initialization
46
 * - if form contains colorpicker(s) we have to initialize them
47
 * - contained date or time pickers also needs initialization
48
 * - embedded CKEditor have to be loaded and configured
49
 * - add Eventlistener to display value of range elements in assigned value label
50
 */
51
window.addEventListener('load', initFormGenerator);
52
53
function initFormGenerator()
54
{
55
    if (g_oConfigFromPHP.Color !== undefined) {
56
        jscolor.presets.default = g_oConfigFromPHP.Color;
57
        jscolor.init();
58
    }
59
    
60
    if (g_oConfigFromPHP.DTSel !== undefined) {
61
    	g_FDP = new FormPicker(g_oConfigFromPHP);
62
        g_FDP.init();
63
    }
64
    
65
    if (g_oConfigFromPHP.CKEditor !== undefined) {
66
        g_oCKEdit = new FormCKEditor(g_oConfigFromPHP, CKEDITOR);
67
        g_oCKEdit.load();
68
    }
69
    
70
	var oRanges = document.querySelector("input[type=range]");
71
	
72
	oRanges.addEventListener('change', rangeChanged);
73
	oRanges.addEventListener('input', rangeChanged);
74
}
75
76
/**
77
 * Event - handler for the 'change' and 'input' event trigerred by each range element in the form.
78
 * If there exist a label element that is assigned to the triggering range through the 'for' attrib,
79
 * the value of the range element is set as content of the label.
80
 */
81
function rangeChanged()
82
{
83
	var oRange = this;
84
	var oLabels = document.getElementsByTagName('label');
85
	for (var i = 0; i < oLabels.length; i++) {
86
	    if (oLabels[i].htmlFor == oRange.id) {
87
	    	oLabels[i].innerHTML = this.value;
88
	    }
89
	}	
90
}
91
92
/**
93
 * Helper function to create Error/Warning-Message in the document.
94
 * The Messeage is only created, if the debug mode is enabled.
95
 * @param string msg text to display
96
 * @param string level level for the message
97
 */
98
function displayJSError(msg, level)
99
{
100
    if (g_oConfigFromPHP.DebugMode) {
101
        let div = document.createElement('div');
102
        div.id = 'JSError';
103
        let header = document.createElement('h1');
104
        div.appendChild(header);
105
        let body = document.createElement('p');
106
        div.appendChild(body);
107
        header.innerHTML = 'Javascript ' + level;
108
        body.innerHTML = msg;
109
        document.body.insertBefore(div, document.body.firstChild);
110
    }
111
}
112
113
/**
114
 * Validate the form.
115
 */
116
function validateForm()
117
{
118
    var FDV = new FormDataValidator(g_oConfigFromPHP.FormDataValidation);
119
    return FDV.validate();
120
}
121
122
/**
123
 * Call the filemanager to browse for a file on the server.
124
 */
125
function browseServer(editID, imgID, strExpand)
126
{
127
    let FmConnector = new RichFmConnector(g_oConfigFromPHP.RichFilemanager.Path);
128
    FmConnector.editID = editID;
129
    FmConnector.imgID = imgID;
130
    FmConnector.browseServerModal(strExpand);
131
}
132
133
/**
134
 * Handler for the DTU button (Date,Time User)
135
 */
136
function onInsertDateTimeUser(id, strUsername)
137
{
138
    let oEdit = document.getElementById(id);
139
    if (oEdit) {
140
        let date = new Date();
141
        let strValue = oEdit.innerHTML = date.toLocaleString();
142
        if (strUsername != '') {
143
            strValue += ' / ' + strUsername;
144
        }
145
        oEdit.value = strValue;
146
    }
147
}
148
149
/**
150
 * Handler for the reset - Button.
151
 * Used to to reset the content of readonly-inputs or images that get the value
152
 * from filebrowser or another picker etc.
153
 */
154
function resetElement(id)
155
{
156
    let oElement = document.getElementById(id);
157
    if (oElement) {
158
        if (oElement.tagName.toLowerCase() === 'img') {
159
            oElement.src = oElement.getAttribute('data-default');
160
            resetElement(oElement.getAttribute('data-bound-to'));
161
        } else {
162
            oElement.value = '';
163
        }
164
    }
165
}
166
167
/**
168
 * Adjust the height of two columns.
169
 */
170
function adjustColumnHeight(col1, col2)
171
{
172
	var oCol1 = document.getElementById(col1);
173
	var oCol2 = document.getElementById(col2);
174
	if (oCol1 && oCol2) {
175
		if (oCol1.offsetHeight > oCol2.offsetHeight) {
176
			oCol2.style.height = oCol1.offsetHeight + 'px'; 
177
		} else {
178
			oCol1.style.height = oCol2.offsetHeight + 'px'; 
179
		}
180
	}
181
}
182
183
/**
184
 * Dynamic loading of additional scripts.
185
 */
186
function loadScript(strScriptfile)
187
{
188
    var oScript = document.createElement('script');
189
    document.head.appendChild(oScript);
190
    // oScript.onload = function() {console.log('script ' + strScriptfile + ' loaded!');};
191
    oScript.type = 'text/javascript';
192
    oScript.src = strScriptfile;
193
}
194
195